package com.cuotigou.controller;

import com.cuotigou.service.MobileCodeService;
import com.cuotigou.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/10/30.
 */
@Controller
@RequestMapping("/mobileCode")
public class MobileCodeController {

    @Autowired
    private MobileCodeService mobileCodeService;

    /**
     * 发送短信
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/sendCode", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult sms_code(XMLBean xmlBean, HttpServletRequest request){
        String ip = CodeUtils.getIpAddress(request);
        return  mobileCodeService.sms_code(xmlBean,ip);

    }

    /**
     * 校验验证码
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/checkMobileCode",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult checkMobileCode(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getMobile())){
            return ObjectResult.build("0","手机号不能为空");
        }
        if (StringUtil.isEmpty(xmlBean.getCode())){
            return ObjectResult.build("0","验证码不能为空");
        }
        return mobileCodeService.checkMobileCode(xmlBean);
    }

}
